import 'package:dulu/components/header.dart';
import 'package:dulu/config/global.dart';
import 'package:dulu/pages/line.dart';
import 'package:dulu/pages/table.dart';
import 'package:flutter/material.dart';

class StatisticsPage extends StatefulWidget {
  StatisticsPage({Key key, this.year, this.month, this.day}) : super(key: key);
  final int year;
  final int month;
  final int day;
  @override
  _StatisticsPageState createState() => _StatisticsPageState();
}

class _StatisticsPageState extends State<StatisticsPage> {
  int year = 2021;
  int month = 1;
  int day = 1;

  @override
  void initState() {
    year = widget.year;
    month = widget.month;
    day = widget.day;
    super.initState();
  }

  int _currentPageIndex = 0;

  _buildPageView() {
    return Center(
      child: Container(
        child: Stack(
          children: <Widget>[
            Positioned(
              top: 0,
              left: 0,
              right: 0,
              bottom: 0,
              child: PageView(
                onPageChanged: (index) {
                  setState(() => {_currentPageIndex = index});
                },
                children: [
                  MoodTablePage(year: year, month: month, day: day),
                  MoodLineChartPage(year: year, month: month, day: day),
                ],
              ),
            ),
            Positioned(
              bottom: 40,
              left: 0,
              right: 0,
              child: Container(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: List.generate(2, (i) {
                    return Container(
                      margin: EdgeInsets.symmetric(horizontal: 5),
                      width: 6,
                      height: 6,
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        color: _currentPageIndex == i
                            ? Global.fontColor
                            : Global.fontSecondColor.withOpacity(.5),
                      ),
                    );
                  }).toList(),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        color: const Color(0xFFFFFFFF),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Header(onDateChange: (date) {
              setState(() {
                year = date.year;
                month = date.month;
              });
            }),
            Expanded(
              child: _buildPageView(),
            ),
          ],
        ),
      ),
    );
  }
}
